from flask import Blueprint, session, redirect, url_for, jsonify, request, render_template

from service import detail_service
from util.request_helper import get_cookie_jar, get_parameter

restaurant_detail = Blueprint('restaurant_detail', __name__)


@restaurant_detail.route("/restaurant_detail")
def detail_page():
    telephone = session.get("telephone")
    base_info = {
        "telephone": telephone
    }
    return render_template('detail.html', info=base_info)


@restaurant_detail.route("/getDetailInfo")
def detail():
    restaurant_id = get_parameter(request, "restaurant_id")
    cookie = session.get("cookie")
    if cookie is None:
        return redirect(url_for('login.login_page'))
    cookie_jar = get_cookie_jar(cookie)
    restaurant_info = detail_service.get_restaurant_info(restaurant_id, cookie_jar)
    restaurant_comments = detail_service.get_restaurant_comments(restaurant_id, cookie_jar)
    return jsonify({
        "restaurant_info": restaurant_info,
        "restaurant_comments": restaurant_comments
    })
